package com.apes.hr.cultivate.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.cultivate.model.*;
import com.apes.hr.cultivate.repository.CourseRepository;
import com.apes.hr.cultivate.repository.CultivatePlanAppraisalRepository;
import com.apes.hr.cultivate.repository.CultivatePlanArrangeRepository;
import com.apes.hr.cultivate.repository.CultivatePlanItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：培训计划安排
 *
 * @author Linwz
 * @create 2019-06-17 15:11
 */
@Service("hrCultivatePlanArrangeService")
public class CultivatePlanArrangeService extends DomainService {
    @Autowired
    public CultivatePlanArrangeRepository cultivatePlanArrangeRepository;
    @Autowired
    public CultivatePlanItemRepository cultivatePlanItemRepository;
    @Autowired
    public CourseRepository courseRepository;
    @Autowired
    CultivatePlanAppraisalRepository appraisalRepository;


    /**
     * 功能：创建
     * @param request
     * @return
     */
    public CultivatePlanArrange save(SimpleRequest request) {
        CultivatePlanArrange cultivatePlanArrange = request.getO(CultivatePlanArrange.class);
        cultivatePlanArrange.setState("create");  //设置当前的状态
        return cultivatePlanArrangeRepository.saveAndFlush(cultivatePlanArrange);
    }


    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public CultivatePlanArrange update(SimpleRequest request) {
        checkData(request);
        CultivatePlanArrange cultivatePlanArrange = request.getO(CultivatePlanArrange.class);
        return cultivatePlanArrangeRepository.saveAndFlush(cultivatePlanArrange);

    }


    /**
     * 功能：审核培训计划安排
     *
     * @param request
     * @return
     */
    public CultivatePlanArrange approve(SimpleRequest request) {
        CultivatePlanArrange oldBill = checkData(request);
        if (oldBill.getApproveDate() != null) throw new RuntimeException("培训计划安排：" + request.get("id") + " 在系统中已审核。");
        CultivatePlanArrange ptType = request.getO(CultivatePlanArrange.class);
        ptType.setState("approve");
        ptType.setApproveDate(new Date());
        ptType.setApproveUid(this.getPerson(request));
        setAppraisalRecords(ptType);
        return cultivatePlanArrangeRepository.saveAndFlush(ptType);
    }

    /**
     * 审核 参训安排后生成个人参训考核单
     * @param arrange
     */
    private void setAppraisalRecords(CultivatePlanArrange arrange){
        CultivatePlan cultivatePlan = arrange.getCultivatePlan();
        // 参训 的培训计划里的 课程 等于 考核的项目
       Set<CultivatePlanItem> planItemSet= cultivatePlan.getCultivatePlanItems();
       planItemSet.forEach(item -> {
           if (!item.isRepeal()) {
               //培训计划考核明细
               Set<CultivatePlanAppraisalItem> cultivatePlanAppraisalItems = new HashSet<>();
               CultivatePlanAppraisal planAppraisal = new CultivatePlanAppraisal();
               planAppraisal.setCourse(item.getCourse());
               planAppraisal.setCultivatePlan(cultivatePlan);
               planAppraisal.setCultivateClass(cultivatePlan.getCultivateClass());
               planAppraisal.setOutCultivatePlanUid(item.getOutCultivatePlanUid());
               planAppraisal.setCultivatePlanPlace(item.getCultivatePlanPlace());
               planAppraisal.setCultivatePlanList(item.getCultivatePlanList());
               planAppraisal.setState("create");
               // 参训人员 == 考核明细
               arrange.getCultivatePlanArrangeItems().forEach(arrangeItem -> {
                   CultivatePlanAppraisalItem planAppraisalItem = new CultivatePlanAppraisalItem();
                   planAppraisalItem.setCultivatePlanAppraisal(planAppraisal);
                   planAppraisalItem.setEmployeeId(arrangeItem.getEmployeeId());
                   planAppraisalItem.setBranch(arrangeItem.getBranch());
                   planAppraisalItem.setWorkPost(arrangeItem.getWorkPost());
                   planAppraisalItem.setJob(arrangeItem.getJob());
                   planAppraisalItem.setRankId(arrangeItem.getRankId());
                   planAppraisalItem.setEntryDate(arrangeItem.getEntryDate());
                   planAppraisalItem.setDuration(arrangeItem.getDuration());
                   planAppraisalItem.setServeStateDate(arrange.getServeStateDate());
                   planAppraisalItem.setServeEndDate(arrange.getServeEndDate());
                   cultivatePlanAppraisalItems.add(planAppraisalItem);
               });
               planAppraisal.setCultivatePlanAppraisalItems(cultivatePlanAppraisalItems);
               appraisalRepository.saveAndFlush(planAppraisal);
           }
       });

    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     * @param request
     */
    public CultivatePlanArrange checkData(SimpleRequest request){
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        CultivatePlanArrange cultivatePlanArrange = cultivatePlanArrangeRepository.findOne(id);
        if (cultivatePlanArrange == null) throw new RuntimeException("培训计划安排【" + id + "】在系统中不存在。");
        if (cultivatePlanArrange.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return cultivatePlanArrange;
    }

    /***
     * 功能： 删除
     * @param request
     * @return
     * @throws RuntimeException
     */
    public String delete(SimpleRequest request) throws RuntimeException {
        checkData(request);
        // 设置删除人以及状态
        CultivatePlanArrange requestCultivatePlan = request.getO(CultivatePlanArrange.class);

        requestCultivatePlan.setDeleteId(this.getPerson(request)); //设置人员
        requestCultivatePlan.setDeleteDate(new Date()); // 设置时间
        requestCultivatePlan.setState("delete");  //设置当前的状态为删除
        cultivatePlanArrangeRepository.saveAndFlush(requestCultivatePlan);

        return "删除成功！";
    }


    /***
     * 功能：回车查询
     * @param request
     * @return
     */
    public CultivatePlanArrange findOne(SimpleRequest request) {
        CultivatePlanArrange cultivatePlanArrange = cultivatePlanArrangeRepository.findOne((String) request.get("id"));
        return cultivatePlanArrange;
    }

}
